package com.tidy.seed.util;

import com.tidy.seed.admin.system.entity.User;

public class UserThreadLocal {
    private static final ThreadLocal<User> LOCAL = new ThreadLocal<>();
    private static final ThreadLocal<Integer[]> ROLE_IDS = new ThreadLocal<>();

    private UserThreadLocal() {
    }

    public static void put(User user) {
        LOCAL.set(user);
    }

    public static User get() {
        return LOCAL.get();
    }

    public static void remove() {
        LOCAL.remove();
        ROLE_IDS.remove();
    }

    public static void putRoleIds(Integer[] roleIds) {
        ROLE_IDS.set(roleIds);
    }

    public static Integer[] getRoleIds() {
        return ROLE_IDS.get();
    }

    public static Integer getUserId() {
        User user = get();
        return user != null ? user.getId() : null;
    }

    public static String getUsername() {
        User user = get();
        return user != null ? user.getUsername() : null;
    }
}
